<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>入库申请登记</title>
    <!-- 新 Bootstrap4 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">

    <link rel="stylesheet" href="../../../../common/css/bootstrap-table.min.css">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>

    <script src="../../../../common/js/bootstrap-table.min.js"></script>
    <script src="../../../../common/js/bootstrap-table-zh-CN.min.js"></script>

    <!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>

    <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>


    <!--    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>-->
    <!--    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css">-->
    <!--    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">-->
    <link href="../../../../static/css/jquery.dataTables.min.css" rel="stylesheet">
    <link href="../../../../static/css/style.css" rel="stylesheet">
    <!--    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>-->
    <link rel="stylesheet" href="../css/productionPlanRegistrationCss.css">

</head>

<body style="background-color: white">
<div style="height: 5px"></div>

<!--<div style="margin-left: 10%">-->
<!--    <button class='btn btn-primary' onclick="f1()">提交申请</button>-->
<!--</div>-->



<div style="display: flex;justify-content: end">
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">添加产品<span
            class="btn-icon-end"><i class="bi bi-plus-circle"></i></span>
    </button>
</div>


<div class="d1">
    <div class="text-center">
        <h2>入库申请单</h2>
    </div>

    <div class="container-fluid row">
        <div class="input-group mb-4 col">
            <div class="input-group-prepend">
                <span class="input-group-text">出库人:</span>
            </div>
            <input type="text" class="form-control" id="storer" placeholder="">
        </div>
        <div class="input-group mb-4 col">
            <div class="input-group-prepend">
                <span class="input-group-text">出库理由:</span>
            </div>
            <select class="default-select  form-control wide" id="reason">
                    <option value="R001-1">生产入库</option>
                    <option value="R001-2">库存初始</option>
                    <option value="R001-3">赠送</option>
                    <option value="R001-4">内部归还</option>
                    <option value="R001-5">其他归还</option>
            </select>
        </div>
    </div>

    <div class="container-fluid row">
        <table class="table table-bordered" style="text-align: center">
            <thead>
            <tr>
                <th>产品名称</th>
                <th>产品编号</th>
                <th>描述</th>
                <th>当前可存放数量</th>
                <th>数量</th>
                <th>单位</th>
                <th>成本单价</th>
                <th>小记</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="tbodyShow">


            </tbody>
        </table>
    </div>

    <div class="container-fluid row" style="margin-top: 50px">
        <div class="input-group mb-4 col">
            <div class="input-group-prepend">
                <span class="input-group-text">总件数:</span>
            </div>
            <input type="text" class="form-control" id="amountSum" readonly>
        </div>
        <div class="input-group mb-4 col">
            <div class="input-group-prepend">
                <span class="input-group-text">总金额:</span>
            </div>
            <input type="text" class="form-control" id="costPriceSum" readonly>
        </div>
    </div>


    <div class="container-fluid row">
        <div class="input-group mb-4 col">
            <div class="input-group-prepend">
                <span class="input-group-text">登记人:</span>
            </div>
            <input type="text" class="form-control" id="register" readonly>
        </div>
        <div class="input-group mb-4 col">
            <div class="input-group-prepend">
                <span class="input-group-text">登记时间:</span>
            </div>
            <input type="text" class="form-control" id="registerTime">
        </div>
    </div>


    <!--    <div class="form-group row">-->
    <!--        <label for="amountSum" class="col-sm-2 col-form-label"> 备注：</label>-->
    <!--        <div class="col-sm-6">-->
    <!--            <textarea id="remark" style="width: 200px"></textarea>-->
    <!--        </div>-->

    <!--    </div>-->

    <div class="col" style="margin-bottom: 10px">
        <label for="amountSum" class="col-sm-2 col-form-label"> 备注：</label>
        <textarea class="form-control" rows="4"  id="remark"
                  style="height: 115px;border-color: #c7c7c7;resize:none"></textarea>
    </div>


</div>
<div style="display: flex;justify-content: end">
    <button type="button" class="btn btn-success" onclick="f1()">提交 <span
            class="btn-icon-end"><i
            class="fa fa-check"></i></span>
    </button>
</div>


<!-- 模态框 -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-lg" style="display: flex">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="card-title">产品</h4>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="table-responsive">
                    <div id="example3_wrapper" class="dataTables_wrapper no-footer">
                        <table id="tad">

                        </table>
                    </div>
                </div>
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-dark" data-dismiss="modal">关闭</button>
            </div>

        </div>
    </div>
</div>
<script>
    $(function () {
        $("#registerTime").val(new Date().toLocaleString())
        queryFile();
        queryUser();
    })

    //获取当前用户名
    function queryUser() {
        $.ajax({
            type:"POST",
            url:"/user.action",
            success:function (data) {
                $("#register").val(data.loginId)
            }
        })
    }


    function queryFile() {
        $("#tad").bootstrapTable({
            method:"get",
            contentType:"application/json;charset=utf-8",
            url:"/queryDFileXiaotan.action",
            pageNumber:"1",
            pageSize:"3",
            striped:true,
            pagination:true,
            sidePagination: "server",
            pageList: [3,5,7,9],
            queryParams:function(parts){
                return{
                    currentPage:(parts.offset/parts.limit)+1,
                    pageSize:parts.limit
                }
            },
            columns:[
                {title:"产品编号",field:"productId"},
                {title:"产品名称",field:"productName"},
                {title:"类型",field:"type",formatter:function (value,row,index) {
                        let but=value="Y001-1"?"商品":"物料";
                        return but;
                    }},
                {title:"计量单位",field:"personalUnit"},
                {title:"成本单价",field:"costPrice"},
                {title:"操作",field:"id",formatter:function (value,row,index) {
                        let cc=JSON.stringify(row);
                        let but="<button class='btn btn-outline-warning' onclick='add("+cc+")'>添加</button>";
                        return but;
                    }}
            ]
        })
    }

    function add(bean){

        var uu=0;
        for (let i=0;i<$(".number").length;i++) {
            let i1=$(".productId:eq("+i+")").text();
            if(i1==bean.productId){
                uu=1;
                break;
            }
        }
        if (uu==0) {
            $.ajax({
                type:"POST",
                url:"/queryByProductIdKey.action?pid="+bean.productId,
                success:function (data2) {
                    $("#tbodyShow").append("<tr>" +
                        "<td class='productName'>" + bean.productName + "</td>" +
                        "<td class='productId'>" + bean.productId + "</td>" +
                        "<td class='productNick'>" + bean.productNick + "</td>" +
                        "<td class='kcc'>"+(parseInt(data2.maxCapacityAmount)-parseInt(data2.amount))+"</td>" +
                        "<td ><input type='text' value='1' onblur='f()' class='number form-control-plaintext'></td>" +
                        "<td id='personalUnit'>" + bean.personalUnit + "</td>" +
                        "<td class='realCostPrice'>" + bean.costPrice + "</td>" +
                        "<td class='reporter'></td>" +
                        "<td><button class='btn btn-danger' onclick='f2(this)'>删除</button></td>"+
                        "</tr>")
                    f()
                }
            })
        }else{
            alert("已添加")
        }

    }

    //计算
    function f() {

        for (let j=0;j<$(".number").length;j++){
            let number2=parseInt($(".number:eq("+j+")").val());
            if(number2<1){
                alert("数量不可少于1件")
                $(".number:eq("+j+")").val("1");
            }

        }


        for (let j=0;j<$(".number").length;j++){
            let kcc=parseInt($(".kcc:eq("+j+")").text());
            let number1=parseInt($(".number:eq("+j+")").val());
            if(number1>kcc){
                alert("已超过可存放上线！")
             $(".number:eq("+j+")").val(kcc);
            }
        }

        let qh=0;
        let jh=0;
        for (let i=0;i<$(".number").length;i++){
            let number=parseInt($(".number:eq("+i+")").val());
            let realCostPrice=$(".realCostPrice:eq("+i+")").text();
            let x=number*realCostPrice;
            $(".reporter:eq("+i+")").text(x);
            jh+=number;
            qh+=x;
        }
        //总件数
        $("#amountSum").val(jh)
        //总金额
        $("#costPriceSum").val(qh)
    }

    function f1() {
        if ($("#storer").val()!=""&&$(".number").length>0){
            let list=[];
            for (let i=0;i<$(".number").length;i++){

                let i1=$(".productId:eq("+i+")").text();
                let i2=$(".productName:eq("+i+")").text();
                let i3=$(".productNick:eq("+i+")").text();
                let i4=$(".number:eq("+i+")").val();
                let i5=$(".personalUnit:eq("+i+")").val();
                let i6=$(".realCostPrice:eq("+i+")").text();
                let i7=$(".reporter:eq("+i+")").text();
                let b={
                    productId:i1,
                    productName:i2,
                    productDescribe:i3,
                    amount:i4,
                    amountUnit:i5,
                    costPrice:i6,
                    subtotal:i7
                }
                // list+=b;
                // list.concat(b)
                list.unshift(b)
            }
            let js={
                storer:$("#storer").val(),
                reason:$("#reason").val(),
                costPriceSum:$("#costPriceSum").val(),
                amountSum:$("#amountSum").val(),
                remark:$("#remark").val(),
                register:$("#register").val(),
                registerTime:$("#registerTime").val(),
                list:list
            }
            // alert(js)
            // console.log(js)
            let s = JSON.stringify(js);
            $.ajax({
                type:'POST',
                url:'/saveSGatherXiaotan.action',
                contentType:"application/json;charset=utf-8",
                data:s,
                success: function () {
                    alert("申请成功")
                    f3()
                }
            })
        }else{
            alert("请添加产品和入库人！")
        }
    }
    function f2(d) {
        d.parentNode.parentNode.remove()
        f()
    }
    function f3() {
        window.location.href="gather.html";
    }
</script>

</body>
</html>